// 获取年龄/月龄
import {dateUtils} from "@/utils/dateUtils";
import store from '@/store/index.js'

export  function getAge(age, birthday) {
  if(Number(age) < 2) {
    var date = new Date();
    var curYear = Number(date.getFullYear()); // 年
    var curMonth = date.getMonth() + 1; // 月
    var curDay = Number(date.getDate()); // 日

    // var birthday = '2021-01-11 00:00:00';
    var birdayYear = Number(birthday.substr(0,4));
    var birdayMonth = Number(birthday.substr(5,2));
    var birdayDay = Number(birthday.substr(8,2));

    var monthAge = 0;
    if(curDay < birdayDay){
      monthAge--;
    }

    if(curYear == birdayYear){
      monthAge += curMonth - birdayMonth;
    }else {
      monthAge += (curYear - birdayYear) * 12 + curMonth - birdayMonth;
    }
    return monthAge;
  }
  return age;
}

export function getAgeAndUnit(age, monthAge) {
  if(age < 3){
    return monthAge + '月龄';
  }
  return age + '岁';
}

//计算时间字符串前，后的时间   60 * 1000
//dateStr传入的时间字符串、time 时长按毫秒计算，type 1 前 2 后
export function getCalculateDateStr(dateStr,time,type) {
  var dtStrDate = new Date(dateStr);
  var newDate = new Date();
  if(type==1){
    newDate.setTime(dtStrDate.getTime() - time);
  }else if(type==2){
    newDate.setTime(dtStrDate.getTime() + time);
  }
  var dtStr=dateUtils(2,newDate);
  return dtStr;
}

//日期比较
// 时间1大于时间2，返回true,否则false

export function dateCompare(date1, date2) {
  let dates1 = new Date(date1);
  let dates2 = new Date(date2);
  return dates1 > dates2 ? true : false;
}

/**
 * 空判断， 不分类型，不考虑传入值为对象情况
 **/
export function isNullEmpty(val){
  if((typeof val) == 'string'){
    return val == '';
  }else if ((typeof val) == 'number' && val != NaN){
    return false;
  }else {
    return true;
  }
}

/**
 * 校验只要是数字（包含正负整数，0以及正负浮点数）就返回true
 **/
export function isNumber(val){
  var regPos = /^[0-9]+.?[0-9]*/; //判断是否是数字。

  return regPos.test(val) ? true : false;
}

/**
 * 校验正负正数就返回true
 **/
export function isIntNumber(val){
  var regPos = / ^\d+$/; // 非负整数
  var regNeg = /^\-[1-9][0-9]*$/; // 负整数

  return regPos.test(val) && regNeg.test(val) ? true :false;
}

export function patientInfoSave(data){
  data.ageOrMonthAge = getAgeAndUnit(data.age,data.month_age);

  store.dispatch("user/restseverFall", data);

  console.log(data);
}

//判断日期是否为大于三天
export function timeInterval(time, interval = 3) {
  if(time == '' || time == undefined){
    return false;
  }
  const a = time.split(' ');
  const b = a[0].split('-');
  const c = a[1].split(':');
  const nowTime = new Date();
  const oldTime = new Date(b[0], b[1] - 1, b[2], c[0], c[1], c[2]);
  const over = (nowTime - oldTime) / (1000 * 60 * 60 * 24) > interval; //大于3 代表是否大于3天，如果想判断30天就把3改成30
  return over;
}

